package com.rongji.dfish.demo.comm.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rongji.dfish.base.Utils;
import com.rongji.dfish.demo.base.DemoBaseController;
import com.rongji.dfish.demo.comm.service.CommService;
import com.rongji.dfish.demo.comm.view.CommView;
import com.rongji.dfish.demo.constant.DemoConstants;
import com.rongji.dfish.demo.entity.CommPost;
import com.rongji.dfish.demo.entity.CommPostReply;
import com.rongji.dfish.demo.entity.PubLoginUser;
import com.rongji.dfish.demo.pub.view.PubView;
import com.rongji.dfish.framework.FilterParam;
import com.rongji.dfish.framework.FrameworkHelper;
import com.rongji.dfish.framework.IdGenerator;
import com.rongji.dfish.framework.plugin.file.service.FileService;
import com.rongji.dfish.ui.command.CommandGroup;
import com.rongji.dfish.ui.command.DialogCommand;
import com.rongji.dfish.ui.command.JSCommand;
import com.rongji.dfish.ui.command.ReplaceCommand;
import com.rongji.dfish.ui.layout.View;

@Controller
@RequestMapping("/comm")
public class CommController extends DemoBaseController {
	@Autowired
	private FileService fileService;
	@Autowired
	private CommService commService;
	
	public FileService getFileService() {
		return fileService;
	}
	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}
	
	public CommService getCommService() {
		return commService;
	}
	public void setCommService(CommService commService) {
		this.commService = commService;
	}
	
	@RequestMapping("/index")
	@ResponseBody
	public Object index(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		FilterParam fp = CommView.getParam4PostList(request);
		View view = CommView.buildPostListView(loginUser,fp);
		return view;
	}
	@RequestMapping("reloadPostList")
	@ResponseBody
	public Object reloadPostList(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		FilterParam fp = CommView.getParam4PostList(request);
		if("1".equals(fp.getValueAsString("showType"))&&loginUser==null){
			return new JSCommand("$.alert('请先登录！');");
		}
		View view=CommView.buildPostListView(loginUser,fp);
		return new ReplaceCommand(view.findNodeById(PubView.MAIN_ROOT));
	}
	@RequestMapping("toPost")
	@ResponseBody
	public Object toPost(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		if(loginUser==null){
			return new JSCommand("$.alert('请先登录！');");
		}
		String plateId=request.getParameter("plateId");
		String postId=request.getParameter("postId");
		DialogCommand dialog = new DialogCommand("toPost", "std", "发帖",  DialogCommand.WIDTH_MEDIUM, DialogCommand.HEIGHT_LARGE, null, "");
		
		CommPost commPost = commService.get(postId);
		if (commPost == null) {
			commPost = new CommPost();
			commPost.setPostPlate(plateId);
		}
		
		dialog.setNode(CommView.buildPostView(commPost));
		return dialog;
	}
	
	@RequestMapping(value = "/savePost")
	@ResponseBody
	public Object savePost(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		CommPost post=new CommPost();
		Date now = new Date();
		bind(request,post);
		CommService commService = FrameworkHelper.getBean(CommService.class);
		post.setCreateTime(now);
		post.setPostCreator(loginUser.getUserId());
		
		post.setPostStatus(DemoConstants.STATUS_VERSION);
		if (Utils.isEmpty(post.getPostIsreply())) {
			post.setPostIsreply("0");
		}
		if(Utils.isEmpty(post.getPostId())){
			post.setPostId(IdGenerator.getSortedId());
			commService.save(post);
		}else{
//			post=commService.getCommPost(postId);
//			bind(request,post);
			commService.update(post);
		}
		String itemJson=Utils.getParameter(request, "attachFile");
		fileService.updateFileLink(itemJson, DemoConstants.FILE_LINK_COMM, post.getPostId());
		CommandGroup cg = new CommandGroup();
//		cg.setPath("/index/mod_community");
		cg.add(new JSCommand("$.dialog(this).ownerView.reload();"));
		cg.add(PubView.getAlertInfo("保存成功!"));
		cg.add(new JSCommand("$.close('toPost');"));
		return cg;
	}
	@RequestMapping("/showReply")
	@ResponseBody
	public Object showReply(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		FilterParam fp = CommView.getParam4ReplyList(request);
		View view = CommView.buildReplyListView(loginUser,fp);
		return new ReplaceCommand(view.findNodeById(PubView.MAIN_ROOT));
	}
	
	@RequestMapping(value = "/saveReply")
	@ResponseBody
	public Object saveReply(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		if(loginUser==null){
			return new JSCommand("$.alert('请先登录！');");
		}
		String replyContent = Utils.getParameter(request, "replyContent");
		String postId = request.getParameter("postId");
		CommPostReply reply=new CommPostReply();
		reply.setReplyId(IdGenerator.getSortedId());
		Date now = new Date(System.currentTimeMillis());
		reply.setCreateTime(now);
		reply.setReplyContent(replyContent);
		reply.setReplyCreator(loginUser.getUserId());
		reply.setPostId(postId);
		CommService commService = FrameworkHelper.getBean(CommService.class);
		commService.saveReply(reply);
		
		FilterParam fp = CommView.getParam4ReplyList(request);
		View view=CommView.buildReplyListView(loginUser, fp);
		CommandGroup cg = new CommandGroup();
		cg.setPath("/index/mod_community");
		cg.add(new ReplaceCommand(view.findNodeById(PubView.MAIN_ROOT)));
		cg.add(PubView.getAlertInfo("保存成功!"));
		cg.add(new JSCommand("$.close('toReply');"));
		return cg;
	}
	@RequestMapping("reloadReplyList")
	@ResponseBody
	public Object reloadReplyList(HttpServletRequest request) throws Exception {
		PubLoginUser loginUser = getLoginUser(request);
		FilterParam fp = CommView.getParam4ReplyList(request);
		View view = CommView.buildReplyListView(loginUser,fp);
		return new ReplaceCommand(view.findNodeById(PubView.MAIN_ROOT));
	}
	@RequestMapping(value = "/setTop")
	@ResponseBody
	public Object setTop(HttpServletRequest request) throws Exception {
		String postId = request.getParameter("postId");
		String setTop = request.getParameter("setTop");
		CommService commService = FrameworkHelper.getBean(CommService.class);
		CommPost post=commService.get(postId);
		post.setIsTop(setTop);
		if("1".equals(setTop)){
			post.setTopTime(new Date());
		}else{
			post.setTopTime(null);
		}
		commService.update(post);
		
		return new JSCommand("VM(this).reload();");
	}
}
